<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author zhangchangshun
 * 用户协议
 */
class common extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        ini_set('date.timezone', 'Asia/Shanghai'); // 'Asia/Shanghai'
        $this->load->model('page_model', 'mpage');
        $this->load->model('upload_model','mupload');
        $this->load->model('category_model','mcategory');
        $this->load->model('claim_model','mclaim');
        $this->load->model('codeimg_model','mcodeimg');

    }

    /**
     * 用户协议
     */
    public function page_detail()
    {
        $info = $this->mpage->get_one(array('id'=>6));
        if(!empty($info)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['data']= $info;
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }else{
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['data']= $info;
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }
    }

    /**
     * 二维码图片
     */
    public function code_img()
    {
        $info = $this->mcodeimg->get_one(array('id'=>7));
        if(!empty($info)){
            if($info['photo']!=''){
                $img = $this->mupload->get_one(array('id'=>$info['photo']));
                $info['imgurl'] = UPLOAD_URL.$img['url'];
            }else{
                $info['imgurl'] ='';
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['data']= $info;
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }else{
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['data']= $info;
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }
    }

    /**
     * BANNER管理
     */
    public function banner_list(){
        $limit = 5;
        $filed = "*";
        $result = $this->db->select($filed)
            ->from("boc_banners")
            ->where(array('audit'=>1))
            ->order_by('sort_id','desc')
            ->limit($limit)
            ->get();
        $banners = $result->result_array();
        if(!empty($banners)){
            foreach($banners as $kk=>$vv){
                if($vv['photo']!=''){
                    $photo1 = $this->mupload->get_one(array('id'=>$vv['photo']));
                    if(!empty($photo1)){
                        $banners[$kk]['img'] = UPLOAD_URL.$photo1['url'];
                    }else{
                        $banners[$kk]['img'] = '';
                    }
                }else{
                    $banners[$kk]['img'] = '';
                }
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['data']= $banners;
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }else{
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['data']= '';
            $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
        }

    }


    /**
     * 市场需求(市场所有)
     */
    public function market_needs(){
        $cates= explode(',',$this->input->post('cate_id'));
        $category = array();
        foreach($cates as $ves){
            if($ves!=''){
                $category[] = $ves;
            }
        }
        if(!empty($category)){
            if($category[0]==30 && count($category)==1){
                $type =1;    //筛选所有的悬赏
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $filed = "*";
                $needs_result = $this->db->select($filed)
                    ->from("boc_needs")
                    ->where(array('claim_status'=>1,'status'=>1,'type'=>$type,'is_deleted'=>0))
                    ->order_by('type asc,countdown asc,created desc')
                    ->limit($limit,$start)
                    ->get();
            }else{
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $filed = "*";
                $needs_result = $this->db->select($filed)
                    ->from("boc_needs")
                    ->where(array('claim_status'=>1,'status'=>1,'in'=>array('cate_id',$category),'is_deleted'=>0))
//                    ->order_by('type asc,countdown asc,created desc')
                    ->order_by('created','desc')
                    ->limit($limit,$start)
                    ->get();
            }
        }else{
            $start = $this->input->post('page');
            $limit = 10;
            $start = $limit*($start-1);
            $filed = "*";
            $needs_result = $this->db->select($filed)
                ->from("boc_needs")
                ->where(array('claim_status'=>1,'status'=>1,'is_deleted'=>0))
//                ->order_by('type asc,countdown asc,created desc')
                ->order_by('created','desc')
                ->limit($limit,$start)
                ->get();
        }
        $needs = $needs_result->result_array();
        if(empty($needs)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['data']= array();
        }else{
            foreach($needs as $key=>$val){
                $cate = $this->mcategory->get_one(array('id'=>$val['cate_id']));
                $needs[$key]['category'] = $cate['title'];
                $needs[$key]['cre_time'] = date('m-d H:i',$val['created']);
                $time = time();
                $needs[$key]['now_time'] = $time;
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['data']= $needs;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
    }

}